const fs = require("fs");
const entry = '..';
var chokidar = require('chokidar');
const read = dir => {
    return new Promise((resolve, reject) => {
        fs.readFile(dir, "utf-8", (err, data) => {
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        });
    });
};

const write = (dir, content) => {
    return new Promise((resolve, reject) => {
        fs.writeFile(dir, content, err => {
            if (err) {
                reject(err);
            }
            resolve();
        });
    });
};
chokidar.watch(entry, {
    ignored: /node_modules/
}).on('all', async (event, path) => {
    path = path.replace(/\\/g,"/") ;
    let pathArr = path.split('.');
    let suffix =pathArr.pop();
    pathArr = pathArr.join('.').split('/');
    if(suffix=='css'){
        let oldP = pathArr.join('/')+'.css';
        let newP = pathArr.join('/')+'.wxss';

        console.time(newP + " " + event)
        await write(newP, await read(oldP));
        console.timeEnd(newP + " " + event)
    }
})